home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap14 / Bricks2 / Bricks2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.2 KB  |  101 lines

  1. /*-----------------------------------------
  2.    BRICKS2.C -- CreateBitmap Demonstration
  3.                 (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName [] = TEXT ("Bricks2") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.  
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("CreateBitmap Demo"), 
  37.                           WS_OVERLAPPEDWINDOW, 
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  54. {
  55.      static BITMAP  bitmap = { 0, 8, 8, 2, 1, 1 } ;
  56.      static BYTE    bits [8][2] = { 0xFF, 0, 0x0C, 0, 0x0C, 0, 0x0C, 0,
  57.                                     0xFF, 0, 0xC0, 0, 0xC0, 0, 0xC0, 0 } ;
  58.      static HBITMAP hBitmap ;
  59.      static int     cxClient, cyClient, cxSource, cySource ;
  60.      HDC            hdc, hdcMem ;
  61.      int            x, y ;
  62.      PAINTSTRUCT    ps ;
  63.      
  64.      switch (message)
  65.      {
  66.      case WM_CREATE:
  67.           bitmap.bmBits = bits ;
  68.           hBitmap = CreateBitmapIndirect (&bitmap) ;
  69.           cxSource = bitmap.bmWidth ;
  70.           cySource = bitmap.bmHeight ;
  71.           return 0 ;
  72.  
  73.      case WM_SIZE:
  74.           cxClient = LOWORD (lParam) ;
  75.           cyClient = HIWORD (lParam) ;
  76.           return 0 ;
  77.  
  78.      case WM_PAINT:
  79.           hdc = BeginPaint (hwnd, &ps) ;
  80.  
  81.           hdcMem = CreateCompatibleDC (hdc) ;
  82.           SelectObject (hdcMem, hBitmap) ;
  83.  
  84.           for (y = 0 ; y < cyClient ; y += cySource)
  85.           for (x = 0 ; x < cxClient ; x += cxSource)
  86.           {
  87.                BitBlt (hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY) ;
  88.           }
  89.  
  90.           DeleteDC (hdcMem) ;
  91.           EndPaint (hwnd, &ps) ;
  92.           return 0 ;
  93.  
  94.      case WM_DESTROY:
  95.           DeleteObject (hBitmap) ;
  96.           PostQuitMessage (0) ;
  97.           return 0 ;
  98.      }
  99.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  100. }
  101.